using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
namespace Microsoft.IW
{
    class BlogSiteMasterPageFeature : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            EventLog eventlog = new EventLog();
            eventlog.Source = "BlogFeatureStaple";
            try
            {
                using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
                {
                    if (currentWeb.MasterUrl.Contains("default.master"))
                    {
                        currentWeb.MasterUrl = currentWeb.MasterUrl.Replace("default.master", "custom.master");
                        currentWeb.CustomMasterUrl = currentWeb.MasterUrl.Replace("default.master", "custom.master");
                        currentWeb.ApplyTheme("none");
                    }
                    currentWeb.Update();
                }
            }
            catch (Exception e)
            {

                eventlog.WriteEntry(String.Format("Error activating MySite Blog master page switcher feature {0} : ", e.Message), EventLogEntryType.Error);
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            EventLog eventlog = new EventLog();
            eventlog.Source = "BlogFeatureStaple";

            try
            {
                using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
                {
                    if (currentWeb.MasterUrl.Contains("custom.master"))
                    {
                        currentWeb.MasterUrl = currentWeb.MasterUrl.Replace("custom.master", "default.master");
                        currentWeb.CustomMasterUrl = currentWeb.MasterUrl.Replace("custom.master", "default.master");
                        currentWeb.ApplyTheme("none");
                    }
                    currentWeb.Update();
                }
            }
            catch (Exception e)
            {

                eventlog.WriteEntry(String.Format("Error deactivating MySite Blog master page switcher feature {0} : ", e.Message), EventLogEntryType.Error);
            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}
